package com.crm.manager.service.impl;

import com.crm.common.core.domain.R;
import com.crm.manager.domain.dto.ActivityStatisticsListDTO;
import com.crm.manager.domain.dto.TimeDto;
import com.crm.manager.domain.vo.ActivityStatisticsListVO;
import com.crm.manager.domain.vo.EChartsVO;
import com.crm.manager.domain.vo.PageVO;
import com.crm.manager.domain.vo.VulnerabilityMapVO;
import com.crm.manager.mapper.Report2Mapper;
import com.crm.manager.service.Report2Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class Report2ServiceImpl implements Report2Service {

    @Autowired
    private Report2Mapper report2Mapper;

    @Override
    public VulnerabilityMapVO getVulnerabilityMap(LocalDate begin, LocalDate end) {
        return report2Mapper.getVulnerabilityMap(begin, end);
    }

    @Override
    public EChartsVO salesStatistics(LocalDate begin, LocalDate end) {
        List<Map> list = report2Mapper.salesStatistics(begin, end);
        EChartsVO eChartsVO = new EChartsVO();
        ArrayList<String> dateList = new ArrayList<>();
        ArrayList<Double> dataList = new ArrayList<>();
        for (Map map : list) {
            dateList.add(map.get("days").toString());
            dataList.add(Double.parseDouble(map.get("money").toString()));
        }
        eChartsVO.setXAxis(dateList);
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "销售统计");
        map.put("data", dataList);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(map);
        eChartsVO.setSeries(objects);
        return eChartsVO;
    }

    @Override
    public PageVO deptStatisticsList(LocalDate begin, LocalDate end, TimeDto dto) {
        PageVO pageVO = new PageVO();
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<Map> list = (Page<Map>) report2Mapper.deptStatisticsList(begin, end);
        pageVO.setTotal(list.getTotal());
        pageVO.setRows(list.getResult());
        return pageVO;
    }

    @Override
    public List<Map> chanelStatistics(LocalDate begin, LocalDate end) {
        return report2Mapper.chanelStatistics(begin, end);
    }

    @Override
    public List<Map> activityStatistics(LocalDate begin, LocalDate end) {
        return report2Mapper.activityStatistics(begin, end);
    }

    @Override
    public PageVO activityStatisticsList(ActivityStatisticsListDTO dto) {
        PageVO pageVO = new PageVO();
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        Page<ActivityStatisticsListVO> list = (Page<ActivityStatisticsListVO>) report2Mapper.activityStatisticsList(dto.getBeginCreateTime(), dto.getEndCreateTime());
        pageVO.setTotal(list.getTotal());
        pageVO.setRows(list.getResult());
        return pageVO;
    }
}
